// ignore_for_file: unused_local_variable, must_be_immutable, unused_import, unnecessary_null_comparison

import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/list_empty_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/network_image_load/image_utils.dart';
import 'package:yt_devops/components/paging/refresh.dart';
import 'package:yt_devops/components/tips.dart';
import 'package:yt_devops/pages/site/sear_button_group/button_group_view.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/site_mains_elec_detail/keep_aliver_wrapper.dart';
import 'package:yt_devops/pages/site/site_controller.dart';
import 'package:yt_devops/pages/site/site_list/site_item_view.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/site/site_services.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/user_entity_manager.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/func_utils.dart';

import '../operation_matainance/widgets/alarm_task_tab.dart';

class SiteHomePage extends GetView<SiteHomeController> {
  SiteHomePage({Key? key}) : super(key: key);
  GlobalKey siteListKey = GlobalKey();
  @override
  final controller = Get.put(SiteHomeController());
  final state = Get.find<SiteHomeController>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SiteHomeController>(
      init: controller,
      builder: (_) {
        return Scaffold(
          backgroundColor: Colour.backgroundColor,
          appBar: PreferredSize(
            preferredSize: Size(
              double.infinity,
              264.h,

            ),
            child: GestureDetector(
              onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
              child: _generateHeader(context),
            ),
          ),
          body: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Visibility(
                visible: controller.siteModelList != null &&
                    controller.siteModelList!.isNotEmpty,
                child: Padding(
                  padding: EdgeInsets.fromLTRB(16.w, 24, 16.w, 12),
                  child: SizedBox(
                    height: 26,
                    child: Text(
                      ResKey.siteList.tr,
                      textAlign: TextAlign.left,
                      style: const TextStyle(
                        fontSize: 18,
                        color: Colour.black,
                        fontWeight: FontWeight.w700,
                      ),
                    ),
                  ),
                ),
              ),
              Expanded(
                child: buildRefreshWidget(
                  builder: _listBody,
                  onRefresh: controller.onRefresh,
                  onLoad: controller.loadMoreData,
                  enablePullUp: controller.pagingState.hasMore,
                  refreshController: controller.refreshController,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _generateHeader(context) {
    Log.d('ScreenUtil().statusBarHeight ===' + ScreenUtil().statusBarHeight .toString());
    return Stack(
      alignment: Alignment.bottomCenter,
      children: <Widget>[
        Container(
          padding: const EdgeInsets.only(bottom: 30),
          child: Container(
            height: 230.h,
            width: double.infinity,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: ImageUtils.getImageProvider(
                    UserEntityManager().systemInfo.appHome!),
                fit: BoxFit.fill,
              ),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  // color: Colors.green,
                  margin: EdgeInsets.only(
                    top: (ScreenUtil().statusBarHeight - 6),
                    bottom: 20,
                  ),
                  height: 44,
                  width: double.infinity,
                  child: Stack(
                    alignment: Alignment.centerLeft,
                    children: [
                      // Visibility(
                      //   visible: !controller.isOwner &&
                      //       controller.selectedIndex == 0,
                    Visibility(
                      visible: FuncUtils.isPassAuthority([PermissionConstant.homeSiteAdd]) &&
                          controller.selectedIndex == 0,
                        child: Positioned(
                          right: 16.w,
                          top:6,
                          child: GestureDetector(
                            onTap: controller.addNewSite,
                            child: const Icon(
                                Icons.add,
                                color: Colour.white,
                                // fill: 1,
                                size: 28,

                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Row(
                  children: <Widget>[
                    Expanded(
                      child: Container(
                        // color: Colors.red,
                        margin: EdgeInsets.only(left: 16.w, top: 0),
                        child: BrnSearchText(
                          maxHeight: 56.h,
                          innerColor: Colour.backgroundColor,
                          hintText: ResKey.enterSiteName.tr,
                          innerPadding: EdgeInsets.fromLTRB(0, 8.h, 0, 8.h),
                          hintStyle: TextStyles.lf16,
                          textStyle: TextStyles.f16,
                          textInputType: TextInputType.text,
                          focusNode: controller.searchSiteFocusNode,
                          searchController: controller.searchController,
                          controller: controller.textController,
                          outSideColor: Colors.transparent,
                          borderRadius: BorderRadius.all(
                            Radius.circular(6.sp),
                          ),
                          onTextCommit: (content) =>
                              controller.getSearchSiteName(content),
                          onTextClear: () {
                            controller.getSearchSiteName("");
                            return false;
                          },
                        ),
                      ),
                    ),
                    // Container(
                    //   margin: EdgeInsets.only(left: 16.w),
                    // ),
                    Padding(
                      padding: EdgeInsets.fromLTRB(12.w, 4, 16.w, 0),
                      child: InkWell(
                        onTap: () => controller.toFilter(context),
                        child: Image.asset(
                          ImageAssets.iconFilter,
                          width: 24.w,
                          height: 24.w,
                          color: Colour.white,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),

        Positioned(
          // top: ScreenUtil().statusBarHeight + 168 - 44,
          child:  SearchButtonGroup(
              checkButtonStatus: controller.getSearchButtonStatus,
              siteNumModel: controller.siteNumModel,
            ),
        ),
      ],
    );
  }

  Widget _listBody() {
    if (controller.siteModelList != null &&
        controller.siteModelList!.isNotEmpty) {
      return buildListView(
        padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
        scrollController: controller.scrollController,
        itemBuilder: (item, index) {
          var data = controller.siteModelList![index];
          return SiteItemView(
            model: data,
            onItemTaped: (model) => controller.goSiteDetail(
              id: model.siteId!,
              type: model.status!,
              siteName: model.name!,
              energyOptions: model.energyOptions,
              monetaryUnitName:model.monetaryUnitName,
              operationDays: model.operationDays,
            ),
            favoriteClick: (model) {
              controller.favoriteOrCancel(model);
            },
          );
        },
        data: controller.siteModelList!,
      );
    } else if (controller.siteModelList != null &&
        controller.siteModelList!.isEmpty == true) {
      return ListEmptyWidget(topPosition: 82);
    } else {
      return Container();
    }
  }
}
